// components/moment/moment.js
var http = require('../../utils/http')
var utils = require('../../utils/util')
Component({
    properties: {
        tableList:Object
    },
    data: {

    },
    methods: {
        //点赞功能
    lookLikeFavourite(e){
        http.get("/mini/testToken","",res=>{
            if(res.statusCode == 401 && e !="look"){
                wx.navigateTo({
                    url: '/pages/login1/login1'
                })
            }else{
                let index = e.currentTarget.dataset.index;
                let temp = this.data.tableList;
                //改变点赞量
                if (temp[index].like) {
                    temp[index].likeNum--;
                }else{
                    temp[index].likeNum++;
                }
                //改变点赞状态
                temp[index].like=!temp[index].like;

                this.setData({
                    'lookLikeFavourite.likeId':e.currentTarget.dataset.id,
                    'lookLikeFavourite.type':'moment',
                    tableList:temp
                })
                // 点赞数据后端持久化
                http.post("/looklikefavourite/addLookLikeFavouriteNum",this.data.lookLikeFavourite,res=>{
                    this.data.lookLikeFavourite = {}
                })
            }
        })
    },
     // 跳转到动态详情页
     toarticleDetails(event) {
        let that = this;
        let moment = event.currentTarget.dataset.moment;
        let index = event.currentTarget.dataset.index;
        //给文章添加在数组列表中的下标
        Object.assign(moment,{'index':index})
        Object.assign(moment,{'topicType':'moment'})
        wx.navigateTo({
            url: '/pages/articleDetails/articleDetails?item=' + encodeURIComponent(JSON.stringify(moment)),
            events:{
                //子页面文章有改变，主页面也跟随改变
                itemChange(data){
                    let index = data.data.index;
                    let temp = that.data.tableList;
                    temp[index].like = data.data.like
                    temp[index].likeNum = data.data.likeNum
                    temp[index].lookNum = data.data.lookNum
                    that.setData({
                        'tableList':temp
                    })
                }
            },
        })
    },
    }
})
